package com.testing.class12;

import com.testing.common.AutoTools;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @Classname FirstPostDemo
 * @Description 类型说明
 * @Date 2022/10/31 21:54
 * @Created by 特斯汀Roy
 */
public class FirstPostDemo {

    public static void main(String[] args) throws IOException {
        // hello/login/{age} 请求
        CloseableHttpClient aDefault = HttpClients.createDefault();
        //请求报文
        HttpPost loginAge=new HttpPost("http://localhost:9090/hello/login/32");
        //请求体编辑
        StringEntity loginParam=new StringEntity("username=Roy&password=123456","utf-8");
//        //设置请求体的格式是urlencoded
//        loginParam.setContentType("application/x-www-form-urlencoded;charset=utf-8");
//        loginParam.setContentEncoding("utf-8");
        //修改请求头content-type为urlencoded
        loginAge.setHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
        //将请求体放到请求报文里。
        loginAge.setEntity(loginParam);
        String loginResult = EntityUtils.toString(aDefault.execute(loginAge).getEntity(), "utf-8");
        AutoTools.log.info(loginResult);

    }
}
